package fr.n7.sma.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JSpinner;
import javax.swing.JWindow;
import javax.swing.SpinnerNumberModel;

public class FirstFrame extends JWindow {

	private static final long serialVersionUID = -2997900571474536053L;

	JSpinner gridX;
	JSpinner gridY;

	JButton createGrid;

	public FirstFrame(){
		super();
		
		JLayeredPane panel = new JLayeredPane();
//		panel.setLayout(null);
		this.add(panel);

		Dimension d;
		try {
			BufferedImage im = ImageIO.read(new File("./resources/FirstFrame.png"));
			JLabel background = new JLabel(new ImageIcon(im));
			d = new Dimension(im.getWidth(), im.getHeight());
			background.setBounds(0, 0, im.getWidth(), im.getHeight());
			panel.add(background, new Integer(0));
			setSize(im.getWidth(), im.getHeight());
		} catch (IOException e) {
			e.printStackTrace();
			d = new Dimension(500, 312);
		}

		Dimension size;
		
		SpinnerNumberModel gridXModel = new SpinnerNumberModel(50, 1, 2000, 10);
		gridX = new JSpinner(gridXModel);
		SpinnerNumberModel gridYModel = new SpinnerNumberModel(50, 1, 2000, 10);
		gridY = new JSpinner(gridYModel);

		JLabel bienvenue = new JLabel("Bienvenue sur SMA");
		Font font = bienvenue.getFont();
		bienvenue.setFont(new Font(font.getName(), Font.PLAIN, 30));
		size = bienvenue.getPreferredSize();
		bienvenue.setBounds(115, 40, size.width, size.height);
		panel.add(bienvenue, new Integer(1));
		
		JLabel dimension = new JLabel("Choisissez la dimension de la grille");
		font = dimension.getFont();
		dimension.setFont(new Font(font.getName(), Font.PLAIN, 14));
		size = dimension.getPreferredSize();
		dimension.setBounds(130, 80, size.width, size.height);
		panel.add(dimension, new Integer(1));
		
		size = gridX.getMinimumSize();
		gridX.setBounds(200, 260, size.width, size.height);
		panel.add(gridX, new Integer(1));
		//grille.add(new JLabel("x"));
		size = gridY.getMinimumSize();
		gridY.setBounds(60, 160, size.width, size.height);
		panel.add(gridY, new Integer(1));

		createGrid = new JButton("X");
		size = createGrid.getPreferredSize();
		createGrid.setBounds(d.width-size.width-2, 2, size.width, size.height);
		panel.add(createGrid, new Integer(1));
		createGrid.addActionListener(new ExitListener());
		
		createGrid = new JButton("Créer la grille");
		size = createGrid.getPreferredSize();
		createGrid.setBounds(d.width-size.width-20, d.height-size.height-20, size.width, size.height);
		panel.add(createGrid, new Integer(1));
		createGrid.addActionListener(new CreateGridButtonListener());

		setLocationRelativeTo(getParent());
	}

	private class ExitListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			FirstFrame.this.dispose();
			System.exit(0);
		}
		
	}
	
	private class CreateGridButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			PrincipalFrame f = new PrincipalFrame((Integer)gridX.getValue(), (Integer)gridY.getValue());
			Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			f.pack();
			f.setSize((int)(screen.width*0.8), (int)(screen.height*0.8));
			f.setLocation((int)(screen.width*0.1), (int)(screen.height*0.1));
			f.setExtendedState(JFrame.MAXIMIZED_BOTH);
			setLocationRelativeTo(getParent());
			f.setVisible(true);
			FirstFrame.this.setEnabled(false);
			FirstFrame.this.dispose();
		}

	}

	public static void main(String[] args) {
		FirstFrame ff = new FirstFrame();
		ff.setVisible(true);
	}
}
